﻿using Bids.Core.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telerik.OpenAccess;
using Telerik.OpenAccess.Metadata;
using Telerik.OpenAccess.Metadata.Fluent;

namespace Bids.Core.DomainObjects
{
	/// <summary>
	/// 任务列表
	/// </summary>
	[Serializable]
	public class User : IEntity
	{
        public User()
        {
            this.Contributions = new List<Contribution>();
            this.Tasks = new List<Task>();
            this.Finances = new List<Finance>();
            this.Moods = new List<Mood>();
            this.DesignerCases=new List<DesignerCase>();
            this.CreateTime = DateTime.Now;
            this.Scores = new List<Score>();
            this.MoodReplys = new List<MoodReply>();
        }
		/// <summary>
		/// 
		/// </summary>
        public virtual int ID { set;  get; }
		/// <summary>
		/// 
		/// </summary>
        public virtual string UserName {   set;  get;}
		/// <summary>
		/// 雇主 或 设计师
		/// </summary>
        public virtual int Type { set;  get; }
		/// <summary>
		/// 年龄
		/// </summary>
        public virtual int Age { set; get;}
		/// <summary>
		/// 省份
		/// </summary>
        public virtual string Province{ set;  get; }
		/// <summary>
		/// 城市
		/// </summary>
        public virtual string City { set;  get; }
		/// <summary>
		/// 区县
		/// </summary>
        public virtual string Area{ set; get;}
		/// <summary>
		/// 详细地址
		/// </summary>
        public virtual string Address {set;  get;}
		/// <summary>
		/// 注册时间
		/// </summary>
        public virtual DateTime CreateTime { set;  get; }
		/// <summary>
		/// 电子邮件
		/// </summary>
        public virtual string Email {  set;   get;}
        /// <summary>
        /// 昵称
        /// </summary>
        public virtual string NickName { get; set; }
        /// <summary>
        /// 电话
        /// </summary>
        public virtual string Phone { get; set; }
        /// <summary>
        /// 头像
        /// </summary>
        public virtual string HeadPortrait { get; set; }
        /// <summary>
        /// QQ
        /// </summary>
        public virtual string QQ { set; get; }
        /// <summary>
        /// 简介
        /// </summary>
        public virtual string Summary { set; get; }
       /// <summary>
        /// 宣言
       /// </summary>
        public virtual string Declaration { set; get; }
        /// <summary>
        /// 毕业学校
        /// </summary>
        public virtual string GraduateSchool { set; get; }
        public virtual string Cases { get; set; }
        public virtual string DisplayName
        {
            get
            {
                if (!string.IsNullOrEmpty(this.NickName))
                    return this.NickName;
                else if (!string.IsNullOrEmpty(this.UserName))
                    return this.UserName;
                else if (!string.IsNullOrEmpty(this.Email))
                    return this.Email;
                return Phone;
            }
        }
        /// <summary>
        /// 银行信息
        /// </summary>
        public virtual string Brank { set; get; }
        public virtual IList<Contribution> Contributions{get;set;}
        public virtual IList<Finance> Finances { get; set; }
        public virtual IList<Task> Tasks {get;set;}
        public virtual IList<Mood> Moods { get; set; }
        public virtual IList<DesignerCase> DesignerCases { get; set; }
        public virtual IList<Score> Scores { get; set; }
        public virtual Login Login { get; set; }
        public virtual IList<MoodReply> MoodReplys { get; set; }
        public virtual UserType UserType
        {
            get {
                return (DomainObjects.UserType)this.Type;
            }
        }

        public static MappingConfiguration<User> CreateConfiguration()
        {
            var threadMapping = new MappingConfiguration<User>();
            threadMapping.MapType(t => new
            {
                t.ID,
                t.UserName,
                t.Type,
                t.Age,
                t.Province,
                t.City,
                t.Area,
                t.Address,
                t.CreateTime,
                t.Email,
                t.NickName,
                t.Phone,
                t.HeadPortrait,
                t.QQ,
                t.Summary,
                t.Declaration,
                t.GraduateSchool,
                t.Brank,
                t.Cases
            }).ToTable("Users");
            threadMapping.HasProperty(t => t.ID).IsIdentity(KeyGenerator.Autoinc);
            threadMapping.HasAssociation(t => t.Tasks).WithOpposite(d => d.Employer).HasConstraint((t, d) => t.ID == d.EmployerId);
            threadMapping.HasAssociation(t => t.Contributions).WithOpposite(d => d.Designer).HasConstraint((t, d) => t.ID == d.DesignerId);
            threadMapping.HasAssociation(t => t.Login).WithOpposite(d => d.User).HasConstraint((t,d)=>t.ID == d.ID);
            return threadMapping;
        }
	}
    public enum UserType
    {
        设计师 = 1,
        雇主 = 2
    }
}

